﻿namespace Triad.DAL
{
    using System.Configuration;
    using System.Web;
    /// <summary>
    /// Database BASE Class
    /// </summary>
    public abstract class DBBase
    {
        static bool _EnableDataContextLogging;
        static string _ConnStr;

        /// <summary>
        /// Gets the data context.
        /// </summary>
        /// <value>The data context.</value>
        protected virtual TriadDataContext DataContext
        {
            get
            {
                var context = new TriadDataContext(ConnStr);
                if (EnableDataContextLogging)
                {
                    context.Log = HttpContext.Current.Response.Output;
                }
                //context.Connection.ConnectionString = ConnStr;
                context.ObjectTrackingEnabled = false; 

                return context;
            }
        }

        /// <summary>
        /// Gets the enable data context logging value from teh web.config.
        /// </summary>
        /// <value>The enable data context logging.</value>
        protected virtual bool EnableDataContextLogging
        {
            get
            {
                if ( _EnableDataContextLogging.Equals(null))
                {
                    string val = ConfigurationManager.AppSettings["EnableDataContextLogging"];
                    _EnableDataContextLogging = bool.Parse(val);
                }
                return _EnableDataContextLogging;
            }
        }

        /// <summary>
        /// Gets the Connectino String from the Web.Config.
        /// </summary>
        /// <value>The Conncetion String.</value>
        protected virtual string ConnStr
        {
            get
            {
                if (_ConnStr == null)
                {
                    _ConnStr = ConfigurationManager.ConnectionStrings["TriadFramework"].ConnectionString;
                }
                return _ConnStr;
            }
        }
    }
}
